﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tools.Core;
using Tools.Core.Entities;
using Tools.Services.Interfaces;

namespace Tools.Services
{
    public class MailMessageService : IMailMessageService
    {
        private readonly DB _db;
        public MailMessageService(DB db)
        {
            _db = db;
        }
        public async Task CreateMessageAsync(MailMessage message)
        {
            _db.MailMessages.Add(message);
            await _db.SaveChangesAsync();
        }

        public async Task DeleteMessageAsync(int id)
        {
            var message = await _db.MailMessages.FirstOrDefaultAsync(m => m.Id == id);
            if (message!=null)
            {
                _db.MailMessages.Remove(message);
                await _db.SaveChangesAsync();
            }
        }

        public async Task<IEnumerable<MailMessage>> GetAllMessagesAsync()
        {
            return await _db.MailMessages.ToListAsync(); ;
        }

        public async Task<MailMessage> GetMessageAsync(int id)
        {
            return await _db.MailMessages.FirstOrDefaultAsync(m => m.Id == id);

        }

        public async Task UpdateMessageAsync(MailMessage message)
        {
            _db.MailMessages.AddOrUpdate(message);
            await _db.SaveChangesAsync();
        }
    }
}
